import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import About from '../views/About.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home/1'
  },
  {
    path: '/home/:id',
    name: 'Home',
    component: Home,
    beforeEnter: (to, from, next) => {
      console.log('路由独享守卫：beforeEnter')
      next()
    }
  },
  {
    path: '/about',
    name: 'About',
    component: About,
    beforeEnter: (to, from, next) => {
      console.log('路由独享守卫：beforeEnter')
      next()
    }
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  console.log('全局前置路由守卫：beforeEach')
  next()
})
router.beforeResolve((to, from, next) => {
  // 这和 router.beforeEach 类似，
  // 区别是在导航被确认之前，同时在所有组件内守卫和异步路由组件被解析之后，解析守卫就被调用。
  console.log('全局前置路由守卫：beforeResolve')
  next()
})

router.afterEach((to, from) => {
  console.log('全局前置路由守卫：afterEach')
})



export default router
